/**
 * Created by MD-3 on 2017/5/25.
 */
function mDevice(id, type, name, marker, polyline, circle, workState, alarmState, updateState, preDeviceId) {
    this.id = id;              //设备ID
    this.type = type;            //设备类型，0-中继，1-终端
    this.name = name;            //设备名称
    this.marker = marker;          //覆盖图标
    this.polyline = polyline;        //连接线
    this.circle = circle;          //圆
    this.workState = workState;      //0-正常，1-异常，2-故障
    this.alarmState = alarmState;     //0-未报警，1-正在报警，2-已报警
    this.updateState = updateState;    //0-待升级，1-正在升级，2-升级完成
    this.preDeviceId = preDeviceId;    //相关联的上一级设备Id
}

function addOverlay() {
    addOverlay_Devices();
}

var deviceMarkerList = [];                    //数据以对象的形式保存在list中
function addOverlay_Devices() {
    var deviceList = deviceList_mock;
    var device;
    var id, type, name, lng, lat, radius, workState, alarmState, updateState, preDeviceId
    var icon,deviceMarker, circle;
    var mDeviceObj;
    var repeaterObjList = [];
    for (var i = 0; i < deviceList.length; i++) {
        device = deviceList[i];
        id = device.id;
        type = device.type;
        name = device.name;
        lng = device.position.lng;
        lat = device.position.lat;
        radius = device.radius;
        workState = device.workState;
        alarmState = device.alarmState;
        updateState = device.updateState;
        preDeviceId = device.preDeviceId;

        icon = getMarkerImg(type, workState, alarmState, updateState);
        deviceMarker = addDeviceMarker(icon, lng, lat, name);
        circle = addDeviceCircle(deviceMarker, radius, workState, alarmState, updateState);
        addMarkerListener(deviceMarker, id);

        mDeviceObj = new mDevice(id, type, name, deviceMarker, null, circle, workState, alarmState, updateState, preDeviceId);
        deviceMarkerList.push(mDeviceObj);
    }
    addOverlay_Polyline();
}


function getMarkerImg(type, workState, alarmState, updateState) {
    var offsetx = -75;
    var image = resource_mock.IMG_MARKER_TYPE1;             //终端
    if (type && type == 0) {
        offsetx = 0;
        image = resource_mock.IMG_MARKER_TYPE0;             //中继
    } else if (alarmState && alarmState == 1) {                //未处理报警
        offsetx = 0;
    } else if (workState && workState == 1) {                  //异常
        offsetx = -100;
    } else if (workState && workState == 2) {                  //故障
        offsetx = -25;
    } else if (updateState && updateState == 1) {             //未更新
        offsetx = -50;
    } else {
        offsetx = -75;
    }
    var icon = new AMap.Icon({
        size: new AMap.Size(25, 32),                                 //图标大小
        image: image,
        imageOffset: new AMap.Pixel(offsetx, -2),                  //图标取图偏移量,可配合size使用
        //imageSize: new AMap.Size(24, 30)                            //图标所用图片大小,会造成无法显示图片？
    })
    return icon;
}

function addDeviceMarker(icon, lng, lat, label) {
    var marker = new AMap.Marker({
        map: map,
        icon: icon,
        position: [lng, lat],
        offset: new AMap.Pixel(-12, -28),
        label: {content: label, offset: new AMap.Pixel(15, 25)},
        topWhenClick: true
    });
    return marker;
}

function addDeviceCircle(deviceMarker, radius, workState, alarmState, updateState) {
    if (!radius) return;
    var lineColor = getStrokeColor(workState, alarmState, updateState);
    var circle = new AMap.Circle({
        map: map,
        center: deviceMarker.getPosition(),      // 圆心位置
        radius: radius,                          //半径
        strokeColor: lineColor,                 //线颜色
        strokeOpacity: 1,                       //线透明度
        strokeWeight: 1,                        //线粗细度
        strokeStyle: "dashed",                 //轮廓线样式，实线:solid，虚线:dashed
        fillColor: lineColor,                   //填充颜色
        fillOpacity: 0.1                        //填充透明度
    });
    return circle;
}

function addOverlay_Polyline() {
    var mDevice,preDevice;
    var workState, alarmState, updateState, preDeviceId;
    var lineArr, polyline, lineColor;
    for (var i in deviceMarkerList) {
        mDevice = deviceMarkerList[i];
        if ('1' == mDevice.type) {
            workState = mDevice.workState;
            alarmState = mDevice.alarmState;
            updateState = mDevice.updateState;

            preDeviceId = mDevice.preDeviceId;
            preDevice = getDeviceById(preDeviceId)

            lineArr = [mDevice.marker.getPosition(), preDevice.marker.getPosition()];
            lineColor = getStrokeColor(workState, alarmState, updateState);
            polyline = new AMap.Polyline({
                map: map,
                path: lineArr,          //设置线覆盖物路径
                strokeColor: lineColor, //线颜色
                strokeOpacity: 1,       //线透明度
                strokeWeight: 2,        //线宽，单位：像素
                strokeStyle: "dashed",   //线样式,实线:solid，虚线:dashed
                strokeDasharray: [10, 5] //补充线样式
            });
            mDevice.polyline = polyline;
        }
    }
}

function addMarkerListener(deviceMarker, id) {
    deviceMarker.on('click', function () {
        openDeviceInfo(id);                 //信息窗口
    });
}

var strokeColorList = [
    strokeColor_mock.COLOR0, strokeColor_mock.COLOR1, strokeColor_mock.COLOR2, strokeColor_mock.COLOR3,
    strokeColor_mock.COLOR4, strokeColor_mock.COLOR5, strokeColor_mock.COLOR6, strokeColor_mock.COLOR7,
    strokeColor_mock.COLOR8, strokeColor_mock.COLOR9, strokeColor_mock.COLOR10];
function getStrokeColor(workState, alarmState, updateState) {
    var index = 3;
    if (alarmState && alarmState == 1) {
        index = 0;
    } else if (workState && workState == 1) {
        index = 4;
    } else if (workState && workState == 2) {
        index = 1;
    } else if (updateState && updateState == 1) {
        index = 2;
    } else {
        index = 3;
    }
    return strokeColorList[index];
}

function getDeviceById(id) {
    if (!id || 0 > id) {
        return;
    }
    var dev;
    $.each(deviceMarkerList, function (idx, device) {
        if (id == device.id) {
            dev = device;
            return true;
        }
    })
    return dev;
}

function getDeviceByName(name) {
    if (!name || ""==name) {
        return;
    }
    var dev;
    $.each(deviceMarkerList, function (idx, device) {
        if (name == device.name) {
            dev = device;
            return true;
        }
    })
    return dev;
}